<?php
namespace app\model;



class Relation {

    static function update($id,$data) {
        if (empty($id)) {
            return false;
        }
        db_update (t_relation,$data, array('id'=>$id));
        return true;
    }

    static function save($data){
        global $_G;
        $insertid = db_insert(t_relation, $data);
        $id = $insertid;
        return $id;
    }


    static function getByThemeId($themeid,$openid){
        global $_G;
        if(empty($themeid) || empty($openid)){
            return;
        }
        $params = array();
        $params[':openid'] = $openid;
        $params[':themeid'] = $themeid;
        $fans = db_find("SELECT * FROM ".tablename(t_relation)."
			WHERE openid = :openid and themeid = :themeid",$params);
        return $fans;
    }


    static function getChildsNumByPOpenid($popenid,$uniacid){
        $params = array();
        $params[':uniacid'] = $uniacid;
        $params[':popenid'] = $popenid;
        $point = db_fetchcolumn("SELECT count(*) FROM ".tablename(t_relation)." as r  
			WHERE r.uniacid= :uniacid and r.popenid = :popenid and r.status = 1 ",$params);
        return intval($point);
    }

    static function getChildsNumByPOpenidAndTimeRange($popenid,$uniacid,$starttime,$endtime){
        $params = array();
        $params[':uniacid'] = $uniacid;
        $params[':popenid'] = $popenid;
        $params[':start'] = $starttime;
        $params[':end'] = $endtime;
        $point = db_fetchcolumn("SELECT count(*) FROM ".tablename(t_relation)." as r  
			WHERE r.uniacid= :uniacid and r.popenid = :popenid and r.status = 1 and r.addtime > :start AND r.addtime < :end ",$params);
        return intval($point);
    }

    static function getTodayChildsNumByPOpenid($popenid,$uniacid){
        $params = array();
        $params[':uniacid'] = $uniacid;
        $params[':popenid'] = $popenid;
        $point = db_fetchcolumn("SELECT count(*) FROM ".tablename(t_relation)." as r  
			WHERE r.uniacid= :uniacid and r.popenid = :popenid and r.status = 1  and to_days(from_unixtime(r.addtime)) = to_days(now()) ",$params);
        return intval($point);
    }


    static function getListByOpenidAndPage($openid,$pagenumber,$pagesize,$uniacid,$keyword=''){
        if (empty($openid)) {
            return false;
        }
        $condition = "";
        if(!empty($keyword)){
            $condition.=" and ( d.username like '%{$keyword}%' or d.nickname like '%{$keyword}%' or d.mobile like '%{$keyword}%') ";
        }

        $sql = "SELECT r.* FROM " . tablename(t_relation) . " as r
                            left join ".tablename(t_distributor)." as d on d.openid = r.openid
                    where r.popenid = :openid and r.uniacid = :uniacid and r.status = 1 {$condition} order by r.id desc 
                            LIMIT " . ($pagenumber - 1) * $pagesize . ',' . $pagesize;
        $list = db_select($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $list;
    }

    static function getWorkerListByOpenidAndPage($openid,$pagenumber,$pagesize,$uniacid){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_relation) . " as r
                    where r.popenid = :openid and r.uniacid = :uniacid and r.status = 1 and r.rank != -1 order by r.id desc 
                            LIMIT " . ($pagenumber - 1) * $pagesize . ',' . $pagesize;
        $list = db_select($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $list;
    }


    static function getTwoLevelChildNumByOpenid($openid,$uniacid,$keyword = ''){
        if (empty($openid)) {
            return false;
        }
        $condition = "";
        if(!empty($keyword)){
            $condition.=" and ( d.username like '%{$keyword}%' or d.nickname like '%{$keyword}%' or d.mobile like '%{$keyword}%') ";
        }
        $sql = "SELECT count(*) FROM " . tablename(t_relation) . " as r
                            left join " . tablename(t_relation) . " as f on f.openid = r.popenid
                            left join ".tablename(t_distributor)." as d on d.openid = r.openid
                    where f.popenid = :openid and f.uniacid = :uniacid and r.uniacid = :uniacid and r.status = 1 {$condition}";
        $row = db_fetchcolumn($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $row;
    }

    static function getTwoLevelChildListByOpenidAndPage($openid,$pagenumber,$pagesize,$uniacid,$keyword=''){
        if (empty($openid)) {
            return false;
        }
        $condition = "";
        if(!empty($keyword)){
            $condition.=" and ( d.username like '%{$keyword}%' or d.nickname like '%{$keyword}%' or d.mobile like '%{$keyword}%') ";
        }
        $sql = "SELECT r.* FROM " . tablename(t_relation) . " as r
                            left join " . tablename(t_relation) . " as f on f.openid = r.popenid
                            left join ".tablename(t_distributor)." as d on d.openid = r.openid
                    where f.popenid = :openid and f.uniacid = :uniacid and r.uniacid = :uniacid and r.status = 1 {$condition} order by r.id desc 
                            LIMIT " . ($pagenumber - 1) * $pagesize . ',' . $pagesize;
        $row = db_select($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $row;
    }




    static function getById($id) {
        if (empty($id)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_relation) . " where id = :id";
        $row = db_find($sql, array(":id"=>$id));
        return $row;
    }


    static function getFansSumByOpenid($openid,$uniacid){
        $params = array();
        $params[':uniacid'] = $uniacid;
        $params[':openid'] = $openid;
        $sql = "SELECT count(*) FROM ".tablename(t_relation)."
			WHERE uniacid= :uniacid and popenid = :openid";
        $point = db_fetchcolumn($sql,$params);
        return intval($point);
    }

    /**
     * 获取当前用户的关系记录，默认普通关系 type 为0
     * @param $openid
     * @param $uniacid
     * @param int $type
     * @return bool
     */
    static function getByOpenId($openid,$uniacid,$type =0) {
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_relation) . " where uniacid = :uniacid and openid = :openid and status = 1 and type =:type";
        $row = db_find($sql,array(":openid"=>$openid,":uniacid"=>$uniacid,":type"=>$type));
        return $row;
    }

    static function getByOpenIdAndPopenid($openid,$uniacid,$popenid) {
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_relation) . " where uniacid = :uniacid and openid = :openid and status = 1 and popenid =:popenid";
        $row = db_find($sql,array(":openid"=>$openid,":uniacid"=>$uniacid,":popenid"=>$popenid));
        return $row;
    }

    static function getByPopenIdAndUid($popenid,$uid,$uniacid) {
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_relation) . " where uniacid = :uniacid and popenid = :openid and uid = :uid and status = 1";
        $row = db_find($sql,array(":openid"=>$popenid,":uniacid"=>$uniacid,":uif"=>$uid));
        return $row;
    }

    static function getChildNumByOpenid($openid,$uniacid,$keyword = ""){
        $params = array();
        $params[':uniacid'] = $uniacid;
        $params[':openid'] = $openid;
        $condition = "";
        if(!empty($keyword)){
            $condition.=" and ( d.username like '%{$keyword}%' or d.nickname like '%{$keyword}%' or d.mobile like '%{$keyword}%') ";
        }
        $point = db_fetchcolumn("SELECT count(*) FROM ".tablename(t_relation)." as r
                                    left join ".tablename(t_distributor)." as d on d.openid = r.openid
			WHERE r.uniacid= :uniacid and r.popenid = :openid and r.status = 1 {$condition}",$params);
        return intval($point);
    }

    static function getWorkerChildNumByOpenid($openid,$uniacid){
        $params = array();
        $params[':uniacid'] = $uniacid;
        $params[':openid'] = $openid;
        $point = db_fetchcolumn("SELECT count(*) FROM ".tablename(t_relation)."
			WHERE uniacid= :uniacid and popenid = :openid and status = 1 and rank != -1",$params);
        return intval($point);
    }

    static function getWorkerChildListByOpenid($openid,$uniacid){
        $params = array();
        $params[':uniacid'] = $uniacid;
        $params[':openid'] = $openid;
        $point = db_select("SELECT * FROM ".tablename(t_relation)."
			WHERE uniacid= :uniacid and popenid = :openid and status = 1 and rank != -1",$params);
        return $point;
    }

    static function getParentByOpenidAndType($openid,$uniacid,$type){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.*,f.nickname,f.avatar FROM " . tablename(t_relation) . " as r
                            left join " . tablename(t_member) . " as f on f.openid = r.popenid
                    where r.openid = :openid and r.uniacid = :uniacid and r.status = 1 and r.type = :type order by r.id desc";
        $row = db_find($sql, array(":openid"=>$openid,":uniacid"=>$uniacid,":type"=>$type));
        return $row;
    }

    static function getOnlyCommonParentByOpenid($openid){
        global $_G;
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.*,f.nickname,f.avatar FROM " . tablename(t_relation) . " as r
                            left join " . tablename(t_member) . " as f on f.openid = r.popenid
                    where r.openid = :openid and r.status = 1 and r.type = 0 order by r.id desc limit 1";
        $row = db_find($sql, array(":openid"=>$openid));
        return $row;
    }

    static function getParentByOpenid($openid,$uniacid,$type = 0){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.*,f.nickname,f.avatar FROM " . tablename(t_relation) . " as r
                            left join " . tablename(t_member) . " as f on f.openid = r.popenid
                    where r.openid = :openid and r.uniacid = :uniacid and r.status = 1 and r.type = :type order by r.id desc";
        $row = db_select($sql, array(":openid"=>$openid,":uniacid"=>$uniacid,":type"=>$type));
        return $row;
    }

    static function getParentDistributorByOpenid($openid,$uniacid,$type = 0){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT f.* FROM " . tablename(t_distributor) . " as f
                            left join " . tablename(t_relation) . " as r on f.openid = r.popenid
                    where r.openid = :openid and r.uniacid = :uniacid and f.status = 1 and f.type = :type order by f.id desc";
        $row = db_find($sql, array(":openid"=>$openid,":uniacid"=>$uniacid,":type"=>$type));
        return $row;
    }

    static function getListByOpenid($openid,$uniacid){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_relation) . " as r
                            left join " . tablename(t_member) . " as f on f.openid = r.openid
                    where r.popenid = :openid and r.uniacid = :uniacid and r.status = 1 order by r.id desc";
        $row = db_select($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $row;
    }


}
